﻿using System;
using System.Linq;
using System.Reflection;

namespace Nomad.WebSync.Service.DAO {
    /// <summary>
    /// Pocket Outlook Contact Entity
    /// </summary>
    [Serializable]
    public partial class Contact {
        /// <summary>
        /// Copy all property values from a non-attached instance from this instance
        /// </summary>
        /// <param name="instance">Source instance of Contact</param>
        public void Copy(Contact instance) {
            // Get a list of properties from this class
            PropertyInfo[] properties = this.GetType().GetProperties();
            foreach (var property in properties) {
                // With the property can be read and write
                if (property.CanRead && property.CanWrite) {
                    // Get the value of source instance for this property
                    var propertyValue = property.GetValue(instance, null);
                    // Set the value on current instance of this property
                    property.SetValue(this, propertyValue, null);
                }
            }
        }

        public Contact Clone() {
            var result = new Contact();
            // Get a list of properties from this class
            PropertyInfo[] properties = this.GetType().GetProperties();
            foreach (var property in properties) {
                // With the property can be read and write
                if (property.CanRead && property.CanWrite) {
                    // Get the value of source instance for this property
                    var propertyValue = property.GetValue(this, null);
                    // Set the value on current instance of this property
                    property.SetValue(result, propertyValue, null);
                }
            }
            return result;
        }

        /// <summary>
        /// Synchronize this instace data with data on database
        /// </summary>        
        public void Synchronize(string username) {
            using (var context = new WebSyncDataContext()) {
                this.State = (byte)States.Synchronized;
                this.Account = context.Accounts.SingleOrDefault(query => query.Username == username);

                var item = context.Contacts.SingleOrDefault(query => query.ID == this.ID);                
                if (item != null)                   
                    item.Copy(this); 
                else
                    context.Contacts.InsertOnSubmit(this);                
                context.SubmitChanges();
            }
        }

        public void Remove() {
            using (var context = new WebSyncDataContext()) {
                var item = context.Contacts.SingleOrDefault(query => query.ID == this.ID);
                if (context != null) {
                    context.Contacts.DeleteOnSubmit(item);
                    context.SubmitChanges();
                }
            }
        }

        public static Contact[] ListByAccount(string username) {
            using (var context = new WebSyncDataContext()) {
                // Create a query with contact from current user
                var query = from item in context.Contacts
                            where item.Account.Username == username
                            orderby item.FileAs
                            select item;

                // Return query result
                return query.ToArray();
            }
        }
    }
}
